﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;

namespace IceDog.UseProcess
{
    class ProcessProvider
    {
        public static Process CreateAppProcess(string appName)
        {
            Process process;
            process = new Process();

            ProcessStartInfo info = process.StartInfo;
            info.CreateNoWindow = true;//不显示程序窗口
            info.UseShellExecute = false;//不使用操作系统shell启动
            info.RedirectStandardOutput = true;//重定向标准输出流
            info.RedirectStandardInput = true;//重定向标准输入流
            info.RedirectStandardError = true;//重定向标准错误流
            info.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;//工作目录
            Encoding encoding = Encoding.UTF8;
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                encoding = Encoding.GetEncoding("GBK");
            }

            //设置三个流的编码
            info.StandardInputEncoding = encoding;
            info.StandardOutputEncoding = encoding;
            info.StandardErrorEncoding = encoding;

            info.FileName = appName;

            return process;
        }
    }
}
